/**
 * create by bill 2019-03-10
 */
function handleParam(da) { //预处理app Callback
    // da = String(da)
    console.log(typeof da)
    if(typeof da != 'object'){
        da = JSON.parse(da)
    }
    return da
}
/**
 * 获取设备信息
 * @param {Function} fun1 成功
 */

export function getEnvironmentInfo(fun1, fun2) {
    let timer = setTimeout(() => {
        fun2(handleParam({resultCode:"10001", result:"获取环境信息失败"}))
    }, 2000);
    window.getEnvironmentInfoSuccess = function(da){
        clearTimeout(timer)
        fun1(handleParam(da))
    }
    window.getEnvironmentInfoError= function(da){
        clearTimeout(timer)
        fun2(handleParam(da))
    }
    window.location.href = 'drsports://getEnvironmentInfo?params={"success":"getEnvironmentInfoSuccess","failed":"getEnvironmentInfoError"}'
}

/**
 * 处理版本信息
 * @param {Object} obj 设备信息 
 * flog true: 不弹框
 *      false: 弹框
 */
// console.log(process.version.VERSION)
export function handleVersions(obj) {
    let version = obj.data.version.split('.'), flog = false, nowVersion = process.version.VERSION.split('.')
    if(version&&version.length){
        if(nowVersion[0]>version[0]){
            flog = false
        }else if(nowVersion[0] == version[0]){
            if(nowVersion[1] > version[1]){
                flog = false
            }else if(nowVersion[1] == version[1]){
                if(nowVersion[2]&&nowVersion[2] > version[2]){
                    flog = false
                }else{
                    flog = true
                }
            }else {
                flog = true
            }
        }else {
            flog = true
        }
    }else {
        flog = false
    }
    return flog
}

/**
 * 获取token
 * webapp获取token
 * fun1 成功回调
 * fun2 失败回调
 */
export function getToken(fun1,fun2) {
    window.successCallback = function (da){
        fun1(handleParam(da))
    }
    window.errorCallback = function (da){
        fun2(handleParam(da))
    }
    window.location.href = 'drsports://getToken?params={"success":"successCallback","failed":"errorCallback"}'
}

/**
 * 判断是否在微信浏览器
 */
export function isWeiXin(){
    //window.navigator.userAgent属性包含了浏览器类型、版本、操作系统类型、浏览器引擎类型等信息，这个属性可以用来判断浏览器类型
    var ua = window.navigator.userAgent.toLowerCase();
    //通过正则表达式匹配ua中是否含有MicroMessenger字符串
    if(ua.match(/MicroMessenger/i) == 'micromessenger'){
        return true;
    }else{
        return false;
    }
}

/**
 * 判断手机上是否安装了app
 */
export function openApp(url) {
    if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) {
        var loadDateTime = new Date();
        window.setTimeout(function() {
            var timeOutDateTime = new Date();
            if (timeOutDateTime - loadDateTime < 5000) {
                window.location = "http://a.app.qq.com/o/simple.jsp?pkgname=cn.drsports.client";//ios下载地址
            } else {
            window.close();
            }
        },2000);
        window.location = url;
      }else if (navigator.userAgent.match(/android/i)) {
        var state = null;
        try {
            window.location = url;
            setTimeout(function(){
                window.location= "http://a.app.qq.com/o/simple.jsp?pkgname=cn.drsports.client"; //android下载地址

            },2000);
        } catch(e) {}
    }
}

/**
 * 
 * @param str 要复制的字符串 
 */
export function copy(str,fun1, fun2) {
    window.copySuccess = function (da){
        fun1(handleParam(da))
    }
    window.copyError = function (da){
        fun2(handleParam(da))
    }
    window.location.href = 'drsports://copy?params={"content":'+str+',"success":"copySuccess","failed":"copyError"}'
}
